package cn.cttic.base.controller;

import java.util.List;

import cn.cttic.base.api.domain.vo.BaseCreatedUserVo;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cttic.common.idempotent.annotation.RepeatSubmit;
import cn.cttic.common.log.annotation.Log;
import cn.cttic.common.web.core.BaseController;
import cn.cttic.common.core.domain.R;
import cn.cttic.common.core.validate.AddGroup;
import cn.cttic.common.core.validate.EditGroup;
import cn.cttic.common.log.enums.BusinessType;
import cn.cttic.common.excel.utils.ExcelUtil;
import cn.cttic.base.api.domain.vo.BaseUserVo;
import cn.cttic.base.api.domain.bo.BaseUserBo;
import cn.cttic.base.service.IBaseUserService;

/**
 * 账户信息
 * 前端访问路由地址为:/user/user
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class BaseUserController extends BaseController {

    private final IBaseUserService baseUserService;

    /**
     * 查询账户信息列表
     */
    @SaCheckPermission("user:user:list")
    @GetMapping("/list")
    public R<List<BaseUserVo>> list(BaseUserBo bo) {
        List<BaseUserVo> list = baseUserService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出账户信息列表
     */
    @SaCheckPermission("user:user:export")
    @Log(title = "账户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseUserBo bo, HttpServletResponse response) {
        List<BaseUserVo> list = baseUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "账户信息", BaseUserVo.class, response);
    }

    /**
     * 获取账户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("user:user:query")
    @GetMapping("/{id}")
    public R<BaseUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(baseUserService.queryById(id));
    }

    /**
     * 新增账户信息
     */
    @SaCheckPermission("user:user:add")
    @Log(title = "账户信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseUserBo bo) {
        return toAjax(baseUserService.insertByBo(bo));
    }

    /**
     * 重设密码
     */
    @SaCheckPermission("user:user:edit")
    @Log(title = "重设密码", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/resetPassword")
    public R<BaseCreatedUserVo> resetPassword(@RequestBody BaseUserBo bo) {
        if (StrUtil.isNotBlank(bo.getId())) {
            return R.ok(baseUserService.ressetPassword(bo.getId()));
        } else {
            return R.fail("id 不可为空");
        }
    }

    /**
     * 修改账户信息
     */
    @SaCheckPermission("user:user:edit")
    @Log(title = "账户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseUserBo bo) {
        return toAjax(baseUserService.updateByBo(bo));
    }

    /**
     * 删除账户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("user:user:remove")
    @Log(title = "账户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(baseUserService.deleteWithValidByIds(List.of(ids), true));
    }
}
